<template>
  <div class="root">
    <div class="left-container">
      <div class="title-container">
        <!-- <img src="@/assets/images/icon.png" width="35" height="35" /> -->
        <span class="title-text">非遗京剧管理后台</span>
      </div>
      <el-menu background-color="#3a56ad" class="el-menu-container" :default-active="defaultActive" text-color="#fff" @select="handleSelect" unique-opened>
        <!-- <el-menu-item index="/admin/dashboard">
          <el-icon>
            <Odometer />
          </el-icon>
          <span>控制台</span>
        </el-menu-item> -->
        <el-sub-menu index="/admin/display">
          <template #title>
            <el-icon>
              <MessageBox />
            </el-icon>
            <span>京剧展示管理</span>
          </template>
          <el-menu-item index="/admin/display/list">
            <el-icon>
              <Discount />
            </el-icon>
            <span>内容管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/sys/config/slide">
            <el-icon>
              <Picture></Picture>
            </el-icon>
            <span>轮播图管理</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/content"><!-- v-if="currAdmin.admin&&currAdmin.admin.role==2"-->
          <template #title>
            <el-icon>
              <ChatDotSquare />
            </el-icon>
            <span>论坛管理</span>
          </template>
          <el-menu-item index="/admin/blog/list">
            <el-icon>
              <Paperclip />
            </el-icon>
            <span>帖子审核</span>
          </el-menu-item>
          <el-menu-item index="/admin/comment/list">
            <el-icon>
              <ChatDotRound />
            </el-icon>
            <span>评论管理</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/question">
          <template #title>
            <el-icon>
              <Document />
            </el-icon>
            <span>趣答管理</span>
          </template>
          <el-menu-item index="/admin/question/list">
            <el-icon>
              <MessageBox />
            </el-icon>
            <span>题库更新</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/course">
          <template #title>
            <el-icon>
              <Files />
            </el-icon>
            <span>学习资源管理</span>
          </template>
          <el-menu-item index="/admin/course/list">
            <el-icon>
              <DocumentCopy />
            </el-icon>
            <span>资源列表</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/activity">
          <template #title>
            <el-icon>
              <Reading />
            </el-icon>
            <span>活动管理</span>
          </template>
          <el-menu-item index="/admin/activity/list">
            <el-icon>
              <Stopwatch />
            </el-icon>
            <span>活动列表</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/user">
          <template #title>
            <el-icon>
              <User />
            </el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/admin/user/list">
            <el-icon>
              <Files />
            </el-icon>
            <span>用户信息管理</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/points">
          <template #title>
            <el-icon>
              <ShoppingCart />
            </el-icon>
            <span>积分商城</span>
          </template>
          <el-menu-item index="/admin/goods/list">
            <el-icon>
              <Goods />
            </el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/order/list">
            <el-icon>
              <Files />
            </el-icon>
            <span>兑换记录</span>
          </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/admin/sys">
          <template #title>
            <el-icon>
              <SetUp />
            </el-icon>
            <span>系统设置</span>
          </template>
          <el-menu-item index="/admin/sys/profile">
            <el-icon>
              <User />
            </el-icon>
            <span>个人信息</span>
          </el-menu-item>
          
          <!-- <el-menu-item index="/admin/sys/config/list">
            <el-icon>
              <Setting />
            </el-icon>
            <span>系统参数</span>
          </el-menu-item> -->
        </el-sub-menu>
        <!-- <span style="color: #fff;">{{ currAdmin.admin }}</span> -->
      </el-menu>
    </div>

    <div class="rigth-container">
      <div class="head-container">
        
        <div class="head-user-container">
          <div>
            <span>系统管理员 </span>
            <strong v-if="currAdmin.admin">{{ currAdmin.admin.realName }}</strong>
          </div>
          <span @click="logout" style="margin-left: 15px; cursor: pointer;">退出登录</span>
        </div>
      </div>

      <div class="page">
        <div class="router-container">
          <router-view />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from "vue";
import { useRouter } from "vue-router";
import { getCache, removeCache } from "@/utils/cache";
import { CacheToken } from "@/constants/cacheKey";

const router = useRouter();
let defaultActive = ref("/workflow/definition");
let user = reactive({});

let dateVal = ref<string>();

// 定义计算属性
const currAdmin = computed(() => getCache(CacheToken));

/**
 * 初始化
 */
const onInit = () => {
  const userInfo = getCache(CacheToken);
  user = userInfo;
  console.log('登录用户：', userInfo)
  // defaultActive.value = router.currentRoute.value.path;
};

/**
 * 退出登录
 */
const logout = () => {
  removeCache(CacheToken);
  console.log('管理员登出！')
  router.replace("/admin/login");
};

/**
 * 菜单选择
 * @param key 路由
 */
const handleSelect = (key: string) => {
  router.push(key);
};

/**
 * 获取时间
 */
const getDate = () => {
  var date = new Date(); // 获取时间
  var year = date.getFullYear(); // 获取年
  var month = date.getMonth() + 1; // 获取月
  var day = date.getDate(); // 获取日
  // 周一返回的是1，周六是6，但是周日是0
  var week = "日一二三四五六".charAt(new Date().getDay());
  dateVal.value = year + "年" + getNum(month) + "月" + getNum(day) + "日" + " 星期" + week;
};

// 如果一个数字则在前面添加0
const getNum = (num: number) => {
  return num < 10 ? "0" + num : num;
};

onMounted(() => {
  getDate();
  onInit();
});
</script>

<style scoped>
.root {
  display: flex;
  height: 100vh;
  width: 100%;
}

.left-container {
  width: 250px !important;
  background: #2a5caa;
  min-width: 250px !important;
}

.rigth-container {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.title-container {
  background: #343844;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title-text {
  margin-left: 10px;
  font-family: "幼圆";
  font-size: 20px;
  font-weight: 300;
  color: white;
}

.el-menu-container {
  height: calc(100vh - 150px);
}

.el-sub-menu{
  background-color: #133081;
}

.el-sub-menu .is-active {
  background: #c63c26;
  /* border-radius: 0 80px 80px 0; */
  color: #fff;
}

/* .el-menu-container :deep() .is-active > ul > .is-active {
  margin-right: 40px;
} */

.head-container {
  height: 60px;
  display: flex;
  width: 100%;
  align-items: center;
  background: #343844;
}

.menu-name {
  font-size: 16px;
  color: #000000;
  margin-left: 20px;
}

.head-user-container {
  position: absolute;
  right: 20px;
  font-size: 14px;
  font-weight: 300;
  color: #fff;
  margin-left: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.head-user-close {
  width: 25px;
  height: 25px;
  margin-left: 10px;
  cursor: pointer;
}

.page {
  padding: 10px;
  height: calc(100vh - 80px);
  background: #f0f2f5;
}

.router-container {
  background-color: white;
  padding: 20px;
}
/* 在全局样式文件中 */
.el-menu-item {
  height: 40px;
  line-height: 40px; /* 确保文本垂直居中 */
}
</style>
